import traceback
import logging
app_logger = logging.getLogger("student")
from lib.douyin.doudian.core.AccessTokenBuilder import AccessTokenBuilder
from lib.douyin.doudian.core.DoudianOpConfig import GlobalConfig
from lib.douyin.doudian.api.sms_send.SmsSendRequest import SmsSendRequest
from lib.douyin.doudian.api.order_orderDetail.OrderOrderDetailRequest import OrderOrderDetailRequest
from lib.douyin.doudian.api.sms_template_apply.SmsTemplateApplyRequest import SmsTemplateApplyRequest
from lib.douyin.doudian.api.sms_sign_apply.SmsSignApplyRequest import SmsSignApplyRequest
import time, json
from hashlib import sha256
import hmac


GlobalConfig.appKey = "7067057640550958605"  # appKey
GlobalConfig.appSecret = "9300e292-2e0a-4b54-ba0f-98a605223fd8"  # appSecret
ShopId = "2614741"
Host = "https://openapi-fxg.jinritemai.com"


def getOrder(shop_order_id):
    """
    获取订单详情
    :param shop_order_id:
    :return:
    """
    # 获取token
    accessToken = AccessTokenBuilder.buildTokenByShopId(ShopId)
    request = OrderOrderDetailRequest()
    param = request.getParams()
    app_logger.error(param)
    param.shop_order_id = shop_order_id
    response = request.execute(accessToken)
    data = response.data
    order_id = data['shop_order_detail']['order_id']
    encrypt_post_tel = data['shop_order_detail']['encrypt_post_tel']
    order_status = data['shop_order_detail']['order_status']
    app_logger.error(encrypt_post_tel)
    # res = sendSms(post_tel=encrypt_post_tel)
    return response


def sendSms(post_tel):
    # 获取token
    token = AccessTokenBuilder.buildTokenByShopId(ShopId)
    # 创建Request对象，假设调用的方法名称是: demo.method
    request = SmsSendRequest()
    param = request.getParams()
    param.sms_account = "1"
    param.sign = "火山测试"
    param.template_id = "ST_60505c68"
    param.template_param = "{'code': '1234'}"
    param.tag = "回执"
    param.post_tel = post_tel
    # param.user_ext_code = "abc"
    # 发起API调用
    response = request.execute(token)
    return response


def templateApply():
    """
    短信模板申请
    :return:
    """
    # 获取token
    accessToken = AccessTokenBuilder.buildTokenByShopId(ShopId)
    request = SmsTemplateApplyRequest()
    param = request.getParams()
    param.sms_account = "1"
    param.template_type = "CN_MKT"
    param.template_name = "课程"
    param.template_content = "您购买的商品已重新发出，关注“XXX”公众号刷新订单获取最新物流信息哦~给您造成不便敬请谅解。"
    response = request.execute(accessToken)
    return response


def signApply():
    """
    短信签名申请
    :return:
    """
    # 获取token
    accessToken = AccessTokenBuilder.buildTokenByShopId(ShopId)
    request = SmsSignApplyRequest()
    param = request.getParams()
    param.sms_account = "1"
    param.sign = "抖音CuiCui崔璀"
    response = request.execute(accessToken)
    return response


def commonCurl(method, access_token, param_json, method_url):
    """
    组装公众参数
    :param method:
    :return:
    """
    timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    p = json.dumps(sorted(param_json))
    paramPattern = 'app_key' + GlobalConfig.appKey + 'method' + method + 'param_json' + p + 'timestamp' + timestamp + 'v' + '2'
    signPattern = GlobalConfig.appSecret + paramPattern + GlobalConfig.appSecret
    digest_str = hmac.new(GlobalConfig.appKey.encode('utf-8'), signPattern.encode('utf-8'), digestmod=sha256).hexdigest()
    httpUrl = Host + method_url + "?app_key="+GlobalConfig.appKey+"&method="+method+"&access_token="+access_token+"&param_json="+p+"&timestamp="+timestamp+"&v=2&sign="+digest_str
    return httpUrl